<script setup>
import { useRouter } from 'vue-router';
import { useStore } from 'Vuex';
// 获取父组件传递过来的值
const props = defineProps(['menuData', 'index'])
// 创建router实例
const router = useRouter()
// 创建store实例
const store = useStore()
// 点击菜单事件
const handleClick = (item,active) => {
    router.push(item.meta.path)
    store.commit('addMenu', item.meta)
    store.commit('changeMenuActive', active)
}

</script>

<template>
    <!-- <el-sub-menu index="1">
        <template #title>
            <el-icon>
                <location />
            </el-icon>
            <span>Navigator One</span>
        </template>
<el-menu-item-group title="Group One">
    <el-menu-item index="1-1">item one</el-menu-item>
    <el-menu-item index="1-2">item two</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="Group Two">
    <el-menu-item index="1-3">item three</el-menu-item>
</el-menu-item-group>
<el-sub-menu index="1-4">
    <template #title>item four</template>
    <el-menu-item index="1-4-1">item one</el-menu-item>
</el-sub-menu>
</el-sub-menu> -->
    <template v-for="(item, index) in props.menuData">
        <!-- 没有子菜单情况 -->
        <el-menu-item v-if="!item.children || item.children.length == 0" 
            :index="`${props.index}-${item.meta.id}`"
            :key="`${props.index}-${item.meta.id}`"
            @click="handleClick(item,`${props.index}-${item.meta.id}`)"
            >
            <!-- <el-icon> <setting /> </el-icon>
            <span>Navigator Four</span> -->
            <el-icon size="20">
                <!-- 动态组件获取图标 -->
                <component :is="item.meta.icon"></component>
            </el-icon>
            <span>{{ item.meta.name }}</span>
        </el-menu-item>
        <!-- 有子菜单情况 -->
        <el-sub-menu v-else :index="`${props.index}-${item.meta.id}`">
            <template #title>
                <el-icon size="20">
                    <!-- 动态组件获取图标 -->
                    <component :is="item.meta.icon"></component>
                </el-icon>
                <span>{{ item.meta.name }}</span>
            </template>
            <!-- 循环调用递归 -->
            <tree-menu :menuData="item.children" :index="`${props.index}-${item.meta.id}`"/>
        </el-sub-menu>
    </template>

</template>

<style scoped lang="less"></style>